package com.tianji.learning.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.api.cache.CategoryCache;
import com.tianji.api.client.course.CatalogueClient;
import com.tianji.api.client.course.CategoryClient;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.client.search.SearchClient;
import com.tianji.api.client.user.UserClient;
import com.tianji.api.dto.course.CataSimpleInfoDTO;
import com.tianji.api.dto.course.CourseSimpleInfoDTO;
import com.tianji.api.dto.user.UserDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.LearningNote;
import com.tianji.learning.domain.query.NotePageQuery;
import com.tianji.learning.mapper.LearningNoteMapper;
import com.tianji.learning.service.ILearningNoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gcl
 * @since 2024-07-28
 */
@Service
@RequiredArgsConstructor
public class LearningNoteServiceImpl extends ServiceImpl<LearningNoteMapper, LearningNote> implements ILearningNoteService {
    private final LearningNoteMapper learningNoteMapper;
    private final CourseClient courseClient;
    private final UserClient userClient;
    private final SearchClient searchClient;
    private final CatalogueClient catalogueClient;
    @Override
    public PageDTO getNotePage(NotePageQuery query) {
        //根据课程名称查课程id
        List<Long> coursesIds = null;
        if (query.getName() != null && !query.getName().isEmpty()) {
            coursesIds = searchClient.queryCoursesIdByName(query.getName());
        }
        //构建分页条件
        Page<LearningNote> page = new Page<>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<LearningNote> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(query.getHidden() != null, LearningNote::getHidden, query.getHidden()).ge(query.getBeginTime() != null, LearningNote::getCreateTime, query.getBeginTime()).le(query.getEndTime() != null, LearningNote::getCreateTime, query.getEndTime()).in(coursesIds != null, LearningNote::getCourseId, coursesIds);
        //分页查询
        Page<LearningNote> notePage = learningNoteMapper.selectPage(page, queryWrapper);
        List<LearningNote> notePageRecords = notePage.getRecords();
        //查询用户名称
        List<Long> userIds = notePageRecords.stream().map(LearningNote::getUserId).collect(Collectors.toList());
        List<UserDTO> userDTOList = userClient.queryUserByIds(userIds);
        Map<Long, String> userIdAndNameMap = null;
        if (!CollUtil.isEmpty(userDTOList)) {
            userIdAndNameMap = userDTOList.stream().collect(Collectors.toMap(UserDTO::getId, UserDTO::getName));
        }
        //查询课程名称
        List<Long> coursesIdList = notePageRecords.stream().map(learningNote -> Long.valueOf(learningNote.getCourseId())).collect(Collectors.toList());
        List<CourseSimpleInfoDTO> simpleCoursesInfoList = courseClient.getSimpleInfoList(coursesIdList);
        Map<Long, String> courseIdAndNameMap = null;
        if (!CollUtil.isEmpty(simpleCoursesInfoList)) {
            courseIdAndNameMap = simpleCoursesInfoList.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, CourseSimpleInfoDTO::getName));
        }
        //查询章节名称
        List<Long> chapterIds = notePageRecords.stream().map(learningNote->Long.valueOf( learningNote.getChapterId())).collect(Collectors.toList());
        List<CataSimpleInfoDTO> simpleChapterInfoDTOS = catalogueClient.batchQueryCatalogue(chapterIds);
        Map<Long, String> chapterdAndNameMap=null;
        if(!CollUtil.isEmpty(simpleChapterInfoDTOS)){
            chapterdAndNameMap = simpleChapterInfoDTOS.stream().collect(Collectors.toMap(CataSimpleInfoDTO::getId, CataSimpleInfoDTO::getName));
        }
        //组装数据
        List<PageNoteVo> pageNoteVos = new ArrayList<>();
        for (LearningNote learningNote : notePageRecords) {
            PageNoteVo pageNoteVo = new PageNoteVo();
            pageNoteVo.setId(learningNote.getId());
            pageNoteVo.setContent(learningNote.getContent());
            pageNoteVo.setHidden(learningNote.getHidden().equals(1));
            pageNoteVo.setUsedTimes(learningNote.getUsedTimes());
            pageNoteVo.setCreateTime(learningNote.getCreateTime());
            if (CollUtil.isNotEmpty(courseIdAndNameMap)) {
                pageNoteVo.setCourseName(courseIdAndNameMap.get(learningNote.getCourseId()));
            }
            if (CollUtil.isNotEmpty(chapterdAndNameMap)) {
                pageNoteVo.setChapterName(chapterdAndNameMap.get(learningNote.getChapterId()));
            }
            if (CollUtil.isNotEmpty(chapterdAndNameMap)) {
                pageNoteVo.setSectionName(chapterdAndNameMap.get(learningNote.getChapterId()));
            }
            if (CollUtil.isNotEmpty(userIdAndNameMap)) {
                pageNoteVo.setAuthorName(userIdAndNameMap.get(learningNote.getUserId()));
            }
        }
        return PageDTO.of(page,pageNoteVos);
    }

}
